package com.example.practice2023.advice;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
public class MyExceptionAdvice {

    // 绑定指定的异常，当对应异常出现的时候，会自动调用绑定的办法，返回异常信息
    @ExceptionHandler(NumberFormatException.class)
    public String checkNumber(Exception e) {
        System.out.println(e);
        return "MyExceptionAdvice 请传入纯数字";
    }

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public String checkArray(Exception e) {
        System.out.println(e);
        return "MyExceptionAdvice 数组下标超界了";
    }

    @ExceptionHandler(SQLException.class)
    public String checkSQL(Exception e) {
        System.out.println(e);
        return "MyExceptionAdvice 数据库sql语句出错了";
    }
}
